﻿using System.Data;
using System.Linq;

namespace msBsc.AppLogic.DataAlc
{
    public class GoodsSearch
    {
        private string _strOrgQuery = "";
        private string _strMallNme = "";
        public GoodsSearch(string strpQuery, string strMallNme)
        {
            _strMallNme = strMallNme;
            if (_strMallNme == string.Empty)
            {
                strNeedSql = "(A.GOODS_STATUS_SID='1' AND A.MALL_NO='0')";
            }
            else
            {
                strNeedSql = "(A.GOODS_STATUS_SID='1' AND A.MALL_NO='" + strMallNme + "')";
            }

            SetQueryStr(strpQuery);
        }

        public string strSorting = "";
        public string strNeedSql = "";
        public int iCurrPageNum = 1;
        private System.Collections.Generic.List<string> lsSql = new System.Collections.Generic.List<string>();
        private System.Collections.Generic.Dictionary<string, string> dicOrgQuery = new System.Collections.Generic.Dictionary<string, string>();
        public System.Collections.Generic.Dictionary<string, string> dicQuery = new System.Collections.Generic.Dictionary<string, string>();
        private System.Collections.Generic.Dictionary<string, string> dicSql = new System.Collections.Generic.Dictionary<string, string>();
        private System.Collections.Generic.Dictionary<string, string> dicAttr = new System.Collections.Generic.Dictionary<string, string>();

        public string GetQueryStr(string strpKey, string strpVal, bool blReplace = true)
        {
            System.Collections.Generic.List<string> lskeytem1 = strpKey.Split(',').ToList();
            System.Collections.Generic.List<string> aaa = (from p in dicQuery
                                                           where blReplace && !lskeytem1.Contains(p.Key) || !blReplace
                                                           select
                                                           p.Key + "=" + p.Value
                           ).ToList();

            string strtem1 = string.Join(",", (from p in dicAttr
                                               where blReplace && !lskeytem1.Contains(p.Key) || !blReplace
                                               select
                                               p.Key + ":" + p.Value
                           ).ToList());
            if (strpKey != string.Empty && strpVal != string.Empty)
            {
                if (strpKey != "sort" && strpKey != "page" && strpKey != "ID" && strpKey != "f" && strpKey != "p1" && strpKey != "p2" && strpKey != "b" && strpKey != "c1" && strpKey != "c2" && strpKey != "c3" && strpKey != "r")
                {
                    strtem1 += strtem1 == string.Empty ? strpKey + ":" + strpVal : "," + strpKey + ":" + strpVal;
                }
                else
                {
                    aaa.Add(strpKey + "=" + strpVal);
                }
            }
            if (strtem1 != string.Empty)
            {
                aaa.Add("attr=" + strtem1);
            }
            return string.Join("&", aaa);
        }

        public void SetQueryStr(string strpQuery)
        {
            _strOrgQuery = strpQuery;
            System.Collections.Generic.List<string[]> lsTem = (from p in strpQuery.Split('&')
                                                               where p.IndexOf("=") != -1
                                                               select
                                                               p.Split('=')).Distinct().ToList();
            dicOrgQuery.Clear();
            for (int i = lsTem.Count - 1; i >= 0; i--)
            {
                if (dicOrgQuery.ContainsKey(lsTem[i][0]))
                {
                    if (dicOrgQuery[lsTem[i][0]] != lsTem[i][1])
                    {
                        if (lsTem[i][0].ToLower() == "f" || lsTem[i][0].ToLower() == "p1" || lsTem[i][0].ToLower() == "p2")
                        {
                            //dicOrgQuery[lsTem[i][0]] =  lsTem[i][1];
                        }
                        else
                        {
                            dicOrgQuery[lsTem[i][0]] = dicOrgQuery[lsTem[i][0]] + "," + lsTem[i][1];
                        }
                    }
                }
                else
                {
                    dicOrgQuery.Add(lsTem[i][0], lsTem[i][1]);
                }
            }
            lsSql.Clear();
            dicQuery.Clear();
            dicAttr.Clear();
            string[] arrtm3211 = new string[dicOrgQuery.Keys.Count];
            dicOrgQuery.Keys.CopyTo(arrtm3211, 0);
            //foreach (string skey in dicOrgQuery.Keys)
            for (int y = arrtm3211.Length - 1; y >= 0; y--)
            {
                string skey = arrtm3211[y];
                string sval = dicOrgQuery[skey].Replace("'", "");
                if (sval.Trim() == string.Empty)
                {
                    continue;
                }
                string strtem2 = "";
                if (skey.ToLower() == "f")
                {
                    if (dicQuery.ContainsKey(skey))
                    {
                        continue;
                    }
                    dicQuery.Add(skey, sval);
                    strtem2 = string.Join("AND", (from p in sval.Split(' ')
                                                  where p.Trim() != string.Empty
                                                  select
                                                  " (A.GOODS_NME LIKE '%" + p + "%') "
                                                      ).Distinct().ToArray());
                    if (strtem2.Trim() != string.Empty)
                    {
                        dicSql.Add(skey, "(" + strtem2 + ")");
                    }
                }
                else if (skey.ToLower() == "sort")
                {
                    if (dicQuery.ContainsKey(skey))
                    {
                        continue;
                    }
                    if (sval.Trim() != string.Empty && !sval.Trim().StartsWith(","))
                    {
                        strSorting = sval.Replace("   ", " ").Replace("  ", " ");
                    }
                }
                else if (skey.ToLower() == "page")
                {
                    if (dicQuery.ContainsKey(skey))
                    {
                        continue;
                    }
                    int dectem11 = 1;
                    if (int.TryParse(sval, out dectem11) && dectem11 > 0)
                    {
                        iCurrPageNum = int.Parse(sval);
                    }
                }
                else if (skey == "ID")
                {
                    if (dicQuery.ContainsKey(skey))
                    {
                        continue;
                    }
                    dicQuery.Add(skey, sval);
                    strtem2 = "(A.COMM_TENANT_ID ='" + sval + "') ";
                    dicSql.Add(skey, strtem2);
                }
                else if (skey.ToLower() == "p1")
                {
                    if (dicQuery.ContainsKey(skey))
                    {
                        continue;
                    }
                    decimal dectem11 = 0M;
                    if (decimal.TryParse(sval, out dectem11))
                    {
                        sval = dectem11.ToString();
                        dicQuery.Add(skey, sval);
                        strtem2 = "(A.GOODS_STRIKE_PRICE >" + sval + ") ";
                        dicSql.Add(skey, strtem2);
                    }
                }
                else if (skey.ToLower() == "p2")
                {
                    if (dicQuery.ContainsKey(skey))
                    {
                        continue;
                    }
                    decimal dectem11 = 0M;
                    if (decimal.TryParse(sval, out dectem11))
                    {
                        sval = dectem11.ToString();
                        dicQuery.Add(skey, sval);
                        strtem2 = "(A.GOODS_STRIKE_PRICE <" + sval + ") ";
                        dicSql.Add(skey, strtem2);
                    }
                }
                else if (skey.ToLower() == "c1")
                {
                    if (dicQuery.ContainsKey(skey))
                    {
                        continue;
                    }
                    dicQuery.Add(skey, sval.Trim());
                    strtem2 = string.Join("OR", (from p in sval.Trim().Split(',')
                                                 where p.Trim() != string.Empty
                                                 select
                                                 " (A.LEVEL_ONE_ID ='" + p + "') "
                                                      ).Distinct().ToArray());
                    if (strtem2.Trim() != string.Empty)
                    {
                        dicSql.Add(skey, "(" + strtem2 + ")");
                    }
                }
                else if (skey.ToLower() == "c2")
                {
                    if (dicQuery.ContainsKey(skey))
                    {
                        continue;
                    }
                    dicQuery.Add(skey, sval.Trim());
                    strtem2 = string.Join("OR", (from p in sval.Trim().Split(',')
                                                 where p.Trim() != string.Empty
                                                 select
                                                 " (A.LEVEL_TWO_ID ='" + p + "') "
                                                      ).Distinct().ToArray());
                    if (strtem2.Trim() != string.Empty)
                    {
                        dicSql.Add(skey, "(" + strtem2 + ")");
                    }
                }
                else if (skey.ToLower() == "c3")
                {
                    if (dicQuery.ContainsKey(skey))
                    {
                        continue;
                    }
                    dicQuery.Add(skey, sval.Trim());
                    strtem2 = string.Join("OR", (from p in sval.Trim().Split(',')
                                                 where p.Trim() != string.Empty
                                                 select
                                                 " (A.LEVEL_THREE_ID ='" + p + "') "
                                                      ).Distinct().ToArray());
                    if (strtem2.Trim() != string.Empty)
                    {
                        dicSql.Add(skey, "(" + strtem2 + ")");
                    }
                }
                else if (skey.ToLower() == "b")
                {
                    if (dicQuery.ContainsKey(skey))
                    {
                        continue;
                    }
                    dicQuery.Add(skey, sval.Trim());
                    strtem2 = string.Join("OR", (from p in sval.Trim().Split(',')
                                                 where p.Trim() != string.Empty
                                                 select
                                                 " (A.BRAND_ID ='" + p + "') "
                                                      ).Distinct().ToArray());
                    if (strtem2.Trim() != string.Empty)
                    {
                        dicSql.Add(skey, "(" + strtem2 + ")");
                    }
                }
                else if (skey.ToLower() == "r")
                {
                    if (dicQuery.ContainsKey(skey))
                    {
                        continue;
                    }
                    dicQuery.Add(skey, sval.Trim());
                    string strRwhere = string.Join("OR", (from p in sval.Trim().Split(',')
                                                          where p.Trim() != string.Empty
                                                          select
                                                          " (R.LABEL_NME ='" + p + "') "
                                                      ).Distinct().ToArray());
                    if (strRwhere.Trim() != string.Empty)
                    {
                        dicSql.Add(skey, "(" + strRwhere + ")");
                    }
                    if (!string.IsNullOrEmpty(strRwhere)) { strtem2 = " (A.GOODS_ID IN (SELECT DISTINCT TOP 600 R.GOODS_ID FROM SHP_GOODS_HOT_LABEL R WHERE " + strRwhere + ")) "; }
                    //if (!string.IsNullOrEmpty(strtem2)) { strtem2 = " (A.GOODS_ID IN (SELECT DISTINCT TOP 600 R.GOODS_ID FROM SHP_GOODS_HOT_LABEL R WHERE " + strtem2 + ")) "; }
                }
                else if (skey.ToLower() == "attr")
                {
                    //string[] arrtem3 = sval.Trim().Split(',').Distinct().ToArray();
                    string[] arrtem3 = (from p in sval.Trim().Split(',')
                                        where p.IndexOf(":") != -1
                                        select
                                        p
                                                     ).Distinct().ToArray();
                    string strTwhere = "";
                    for (int r = arrtem3.Length - 1; r >= 0; r--)
                    {
                        string[] arrtem4 = arrtem3[r].Split(':');
                        dicAttr.Add(arrtem4[0], arrtem4[1]);
                        //dicQuery.Add(skey + "_" + arrtem4[0], skey + "=" + arrtem3[r]);
                        strTwhere += (strTwhere == string.Empty ? "(T.ATTR_TEMPLATE_ID='" + arrtem4[0] + "' AND T.ATTRIBUTE_VALUE='" + arrtem4[1] + "')" : " OR " + "(T.ATTR_TEMPLATE_ID='" + arrtem4[0] + "' AND T.ATTRIBUTE_VALUE='" + arrtem4[1] + "')");
                    }
                    if (strTwhere.Trim() != string.Empty)
                    {
                        dicSql.Add(skey, "(" + strTwhere + ")");
                    }
                    //dicSql.Add(skey, strTwhere);
                    if (!string.IsNullOrEmpty(strTwhere)) { strtem2 = " (A.GOODS_ID IN (SELECT DISTINCT TOP 600 T.GOODS_ID FROM SHP_GOODS_ATTRIBUTE T WHERE " + strTwhere + ")) "; }
                }

                if (!string.IsNullOrEmpty(strtem2)) { lsSql.Add("(" + strtem2 + ")"); }
            }
            lsSql.Add(strNeedSql);
        }

        private System.Collections.Generic.Dictionary<string, string> lsSelLinkKey = new System.Collections.Generic.Dictionary<string, string>();

        /// <summary>
        /// 查询头部导航，返回[]
        /// </summary>
        /// <returns></returns>
        public string GetSelectedLinkJson()
        {
            lsSelLinkKey.Clear();
            using (Epm.AppLogic.Framework.CDbAccess oCDbAccess = new Epm.AppLogic.Framework.CDbAccess())
            {
                System.Text.StringBuilder sbJson = new System.Text.StringBuilder();
                int icount = 0;
                sbJson.Append("[");
                if (dicSql.ContainsKey("c3"))
                {
                    DataSet dstem = oCDbAccess.QueryBySQL(@"SELECT NME=dbo.strJoin(A.LEVEL_THREE_NME),C2_ID=dbo.strJoin(B.LEVEL_TWO_ID),C2_NME=dbo.strJoin(B.LEVEL_TWO_NME),C1_ID=dbo.strJoin(C.LEVEL_ONE_ID),C1_NME=dbo.strJoin(C.LEVEL_ONE_NME) FROM SHP_GOODS_TYPE_LEVEL_THREE A 
    LEFT OUTER JOIN SHP_GOODS_TYPE_LEVEL_TWO B ON A.LEVEL_TWO_ID=B.LEVEL_TWO_ID
    LEFT OUTER JOIN SHP_GOODS_TYPE_LEVEL_ONE C ON B.LEVEL_ONE_ID=C.LEVEL_ONE_ID
    WHERE " + dicSql["c3"]);
                    if (dstem.Tables[0].Rows.Count > 0)
                    {
                        if (icount > 0)
                        {
                            sbJson.Append(",");
                        }
                        string strSelTxt = msBsc.AppLogic.Cfg.HttpTools.stringToJson(dstem.Tables[0].Rows[0]["C1_NME"].ToString());
                        sbJson.Append("{");
                        sbJson.Append("\"type_id\":\"c1\"");
                        sbJson.Append(",\"type_nme\":\"c1\"");
                        sbJson.Append(",\"val\":\"" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(dstem.Tables[0].Rows[0]["C1_ID"].ToString()) + "\"");
                        sbJson.Append(",\"txt\":\"" + strSelTxt + "\"");
                        sbJson.Append(",\"del_query\":\"" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(this.GetQueryStr("c1,c2,c3", "")) + "\"");
                        sbJson.Append("}");
                        icount++;

                        if (icount > 0)
                        {
                            sbJson.Append(",");
                        }
                        strSelTxt = msBsc.AppLogic.Cfg.HttpTools.stringToJson(dstem.Tables[0].Rows[0]["C2_NME"].ToString());
                        sbJson.Append("{");
                        sbJson.Append("\"type_id\":\"c2\"");
                        sbJson.Append(",\"type_nme\":\"c2\"");
                        sbJson.Append(",\"val\":\"" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(dstem.Tables[0].Rows[0]["C2_ID"].ToString()) + "\"");
                        sbJson.Append(",\"txt\":\"" + strSelTxt + "\"");
                        sbJson.Append(",\"del_query\":\"" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(this.GetQueryStr("c2,c3", "")) + "\"");
                        sbJson.Append("}");
                        icount++;

                        if (icount > 0)
                        {
                            sbJson.Append(",");
                        }
                        strSelTxt = msBsc.AppLogic.Cfg.HttpTools.stringToJson(dstem.Tables[0].Rows[0]["NME"].ToString());
                        sbJson.Append("{");
                        sbJson.Append("\"type_id\":\"c3\"");
                        sbJson.Append(",\"type_nme\":\"c3\"");
                        sbJson.Append(",\"val\":\"" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(dicQuery["c3"]) + "\"");
                        sbJson.Append(",\"txt\":\"" + strSelTxt + "\"");
                        sbJson.Append(",\"del_query\":\"" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(this.GetQueryStr("c3", "")) + "\"");
                        sbJson.Append("}");
                        icount++;
                        lsSelLinkKey.Add("c3", dicQuery["c3"]);
                    }
                }
                else if (dicSql.ContainsKey("c2"))
                {
                    DataSet dstem = oCDbAccess.QueryBySQL(@"SELECT NME=dbo.strJoin(A.LEVEL_TWO_NME),C1_ID=dbo.strJoin(C.LEVEL_ONE_ID),C1_NME=dbo.strJoin(C.LEVEL_ONE_NME)  FROM SHP_GOODS_TYPE_LEVEL_TWO A 
    LEFT OUTER JOIN SHP_GOODS_TYPE_LEVEL_ONE C ON A.LEVEL_ONE_ID=C.LEVEL_ONE_ID WHERE " + dicSql["c2"]);
                    if (dstem.Tables[0].Rows.Count > 0)
                    {
                        if (icount > 0)
                        {
                            sbJson.Append(",");
                        }
                        string strSelTxt = msBsc.AppLogic.Cfg.HttpTools.stringToJson(dstem.Tables[0].Rows[0]["C1_NME"].ToString());
                        sbJson.Append("{");
                        sbJson.Append("\"type_id\":\"c1\"");
                        sbJson.Append(",\"type_nme\":\"c1\"");
                        sbJson.Append(",\"val\":\"" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(dstem.Tables[0].Rows[0]["C1_ID"].ToString()) + "\"");
                        sbJson.Append(",\"txt\":\"" + strSelTxt + "\"");
                        sbJson.Append(",\"del_query\":\"" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(this.GetQueryStr("c1,c2,c3", "")) + "\"");
                        sbJson.Append("}");
                        icount++;

                        if (icount > 0)
                        {
                            sbJson.Append(",");
                        }
                        strSelTxt = msBsc.AppLogic.Cfg.HttpTools.stringToJson(dstem.Tables[0].Rows[0]["NME"].ToString());
                        sbJson.Append("{");
                        sbJson.Append("\"type_id\":\"c2\"");
                        sbJson.Append(",\"type_nme\":\"c2\"");
                        sbJson.Append(",\"val\":\"" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(dicQuery["c2"]) + "\"");
                        sbJson.Append(",\"txt\":\"" + strSelTxt + "\"");
                        sbJson.Append(",\"del_query\":\"" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(this.GetQueryStr("c2,c3", "")) + "\"");
                        sbJson.Append("}");
                        icount++;
                        lsSelLinkKey.Add("c2", dicQuery["c2"]);
                    }
                }
                else if (dicSql.ContainsKey("c1"))
                {
                    DataSet dstem = oCDbAccess.QueryBySQL("SELECT NME=dbo.strJoin(A.LEVEL_ONE_NME) FROM SHP_GOODS_TYPE_LEVEL_ONE A WHERE " + dicSql["c1"]);
                    if (dstem.Tables[0].Rows.Count > 0)
                    {
                        if (icount > 0)
                        {
                            sbJson.Append(",");
                        }
                        string strSelTxt = msBsc.AppLogic.Cfg.HttpTools.stringToJson(dstem.Tables[0].Rows[0]["NME"].ToString());
                        sbJson.Append("{");
                        sbJson.Append("\"type_id\":\"c1\"");
                        sbJson.Append(",\"type_nme\":\"c1\"");
                        sbJson.Append(",\"val\":\"" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(dicQuery["c1"]) + "\"");
                        sbJson.Append(",\"txt\":\"" + strSelTxt + "\"");
                        sbJson.Append(",\"del_query\":\"" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(this.GetQueryStr("c1,c2,c3", "")) + "\"");
                        sbJson.Append("}");
                        icount++;
                        lsSelLinkKey.Add("c1", dicQuery["c1"]);
                    }
                }

                if (dicSql.ContainsKey("b"))
                {
                    DataSet dstem = oCDbAccess.QueryBySQL("SELECT BRAND_NME=dbo.strJoin(BRAND_NME+'/'+ISNULL(BRAND_NME_EN,'')) FROM SHP_GOODS_BRAND A WHERE " + dicSql["b"]);
                    if (dstem.Tables[0].Rows.Count > 0)
                    {
                        if (icount > 0)
                        {
                            sbJson.Append(",");
                        }
                        string strSelTxt = msBsc.AppLogic.Cfg.HttpTools.stringToJson(dstem.Tables[0].Rows[0]["BRAND_NME"].ToString());
                        sbJson.Append("{");
                        sbJson.Append("\"type_id\":\"b\"");
                        sbJson.Append(",\"type_nme\":\"品牌\"");
                        sbJson.Append(",\"val\":\"" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(dicQuery["b"]) + "\"");
                        sbJson.Append(",\"txt\":\"" + strSelTxt + "\"");
                        sbJson.Append(",\"del_query\":\"" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(this.GetQueryStr("b", "")) + "\"");
                        sbJson.Append("}");
                        icount++;
                        lsSelLinkKey.Add("b", dicQuery["b"]);
                    }
                }

                if (dicSql.ContainsKey("r"))
                {
                    //DataSet dstem = oCDbAccess.QueryBySQL("SELECT NME=dbo.strJoin(R.LABEL_NME) FROM SHP_GOODS_HOT_LABEL R WHERE " + dicSql["r"]);
                    //if (dstem.Tables[0].Rows.Count > 0)
                    //{
                    if (icount > 0)
                    {
                        sbJson.Append(",");
                    }
                    string strSelTxt = msBsc.AppLogic.Cfg.HttpTools.stringToJson(dicQuery["r"]);
                    sbJson.Append("{");
                    sbJson.Append("\"type_id\":\"r\"");
                    sbJson.Append(",\"type_nme\":\"选购热点\"");
                    sbJson.Append(",\"val\":\"" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(dicQuery["r"]) + "\"");
                    sbJson.Append(",\"txt\":\"" + strSelTxt + "\"");
                    sbJson.Append(",\"del_query\":\"" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(this.GetQueryStr("r", "")) + "\"");
                    sbJson.Append("}");
                    icount++;
                    lsSelLinkKey.Add("r", dicQuery["r"]);
                    //}
                }

                if (dicSql.ContainsKey("attr") && dicAttr.Count > 0)
                {

                    DataSet dstem = oCDbAccess.QueryBySQL("SELECT ATTR_TEMPLATE_ID,ATTRIBUTE_NME FROM SHP_GOODS_ATTRIBUTE_TEMPLATE T WHERE ATTR_TEMPLATE_ID IN ('" + string.Join("','", dicAttr.Keys.ToList()) + "')");
                    for (int i = 0; i < dstem.Tables[0].Rows.Count; i++)
                    {
                        if (icount > 0)
                        {
                            sbJson.Append(",");
                        }
                        string strSelVal = msBsc.AppLogic.Cfg.HttpTools.stringToJson(dstem.Tables[0].Rows[i]["ATTR_TEMPLATE_ID"].ToString() + ":" + dicAttr[dstem.Tables[0].Rows[i]["ATTR_TEMPLATE_ID"].ToString()]);
                        string strSelTxt = msBsc.AppLogic.Cfg.HttpTools.stringToJson(dicAttr[dstem.Tables[0].Rows[i]["ATTR_TEMPLATE_ID"].ToString()]);
                        sbJson.Append("{");
                        sbJson.Append("\"type_id\":\"attr\"");
                        sbJson.Append(",\"type_nme\":\"" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(dstem.Tables[0].Rows[i]["ATTRIBUTE_NME"].ToString()) + "\"");
                        sbJson.Append(",\"val\":\"" + strSelVal + "\"");
                        sbJson.Append(",\"txt\":\"" + strSelTxt + "\"");
                        sbJson.Append(",\"del_query\":\"" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(this.GetQueryStr(dstem.Tables[0].Rows[i]["ATTR_TEMPLATE_ID"].ToString(), "")) + "\"");
                        sbJson.Append("}");
                        icount++;
                        lsSelLinkKey.Add(dstem.Tables[0].Rows[i]["ATTR_TEMPLATE_ID"].ToString(), dicAttr[dstem.Tables[0].Rows[i]["ATTR_TEMPLATE_ID"].ToString()]);
                    }
                }
                sbJson.Append("]");


                return sbJson.ToString();
            }
        }

        public string GetVehicleLinkJosn()
        {
            lsSelLinkKey.Clear();
            System.Text.StringBuilder sbJson = new System.Text.StringBuilder();
            int icount = 0;
            string strSelTxt = msBsc.AppLogic.Cfg.HttpTools.stringToJson("1111");
            sbJson.Append("[");
            sbJson.Append("{");
            sbJson.Append("\"type_id\":\"c1\"");
            sbJson.Append(",\"type_nme\":\"c1\"");
            sbJson.Append(",\"val\":\"" + msBsc.AppLogic.Cfg.HttpTools.stringToJson("123456") + "\"");
            sbJson.Append(",\"txt\":\"" + strSelTxt + "\"");
            sbJson.Append(",\"del_query\":\"" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(this.GetQueryStr("c1,c2,c3", "")) + "\"");
            sbJson.Append("}");
            return sbJson.ToString();
        }

        /// <summary>
        /// 查询头部过滤条件，返回[]
        /// </summary>
        /// <returns></returns>
        public string GetFilterDataJson()
        {
            using (Epm.AppLogic.Framework.CDbAccess oCDbAccess = new Epm.AppLogic.Framework.CDbAccess())
            {
                System.Text.StringBuilder sbJson = new System.Text.StringBuilder();
                sbJson.Append("[");
                int icount = 0;
                if (!lsSelLinkKey.ContainsKey("b"))
                {
                    if (icount > 0)
                    {
                        sbJson.Append(",");
                    }
                    sbJson.Append("{");
                    sbJson.Append("\"type_id\":\"b\"");
                    sbJson.Append(",\"type_nme\":\"" + "品牌" + "\"");
                    sbJson.Append(",\"rows\":[");

                    DataSet dstem = oCDbAccess.QueryBySQL("SELECT DISTINCT TOP 20 ID=A.BRAND_ID,NAME=B.BRAND_NME+'/'+ISNULL(B.BRAND_NME_EN,''),IMG_URL=B.LOGO_IMG_URL FROM SHP_GOODS_INFO A LEFT OUTER JOIN SHP_GOODS_BRAND B ON A.BRAND_ID=B.BRAND_ID WHERE B.BRAND_ID IS NOT NULL "
                         + (lsSql.Count > 0 ? "AND " + string.Join(" AND ", lsSql) : ""));
                    for (int i = 0; i < dstem.Tables[0].Rows.Count; i++)
                    {
                        if (i > 0)
                        {
                            sbJson.Append(",");
                        }
                        string strSelVal = msBsc.AppLogic.Cfg.HttpTools.stringToJson(dstem.Tables[0].Rows[i]["ID"].ToString());
                        string strSelTxt = msBsc.AppLogic.Cfg.HttpTools.stringToJson(dstem.Tables[0].Rows[i]["NAME"].ToString());
                        string strSelImg = msBsc.AppLogic.Cfg.HttpTools.stringToJson(dstem.Tables[0].Rows[i]["IMG_URL"].ToString());
                        sbJson.Append("{");
                        sbJson.Append("\"val\":\"" + strSelVal + "\"");
                        sbJson.Append(",\"txt\":\"" + strSelTxt + "\"");
                        sbJson.Append(",\"img\":\"" + strSelImg + "\"");
                        sbJson.Append(",\"query\":\"" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(this.GetQueryStr("b", dstem.Tables[0].Rows[i]["ID"].ToString())) + "\"");
                        sbJson.Append("}");
                    }
                    sbJson.Append("]");
                    sbJson.Append("}");
                    icount++;
                }

                if (1 == 1)
                {
                    if (icount > 0)
                    {
                        sbJson.Append(",");
                    }
                    sbJson.Append("{");
                    sbJson.Append("\"type_id\":\"r\"");
                    sbJson.Append(",\"type_nme\":\"" + "选购热点" + "\"");
                    sbJson.Append(",\"rows\":[");
                    DataSet dstem = oCDbAccess.QueryBySQL("SELECT DISTINCT TOP 20 ID=R.LABEL_NME FROM SHP_GOODS_INFO A LEFT OUTER JOIN SHP_GOODS_HOT_LABEL R ON A.GOODS_ID=R.GOODS_ID WHERE R.LABEL_NME IS NOT NULL " + (dicQuery.ContainsKey("r") ? " AND R.LABEL_NME NOT IN ('" + dicQuery["r"].Replace(",", "',") + "') " : "")
                         + (lsSql.Count > 0 ? "AND " + string.Join(" AND ", lsSql) : ""));
                    for (int i = 0; i < dstem.Tables[0].Rows.Count; i++)
                    {
                        if (i > 0)
                        {
                            sbJson.Append(",");
                        }
                        string strSelVal = msBsc.AppLogic.Cfg.HttpTools.stringToJson(dstem.Tables[0].Rows[i]["ID"].ToString());
                        string strSelTxt = strSelVal;
                        string strSelImg = "";
                        sbJson.Append("{");
                        sbJson.Append("\"val\":\"" + strSelVal + "\"");
                        sbJson.Append(",\"txt\":\"" + strSelTxt + "\"");
                        sbJson.Append(",\"img\":\"" + strSelImg + "\"");
                        sbJson.Append(",\"query\":\"" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(this.GetQueryStr("r", dstem.Tables[0].Rows[i]["ID"].ToString())) + "\"");
                        sbJson.Append("}");
                    }
                    sbJson.Append("]");
                    sbJson.Append("}");
                    icount++;
                }

                if (!lsSelLinkKey.ContainsKey("c3") && lsSelLinkKey.ContainsKey("c2"))
                {
                    if (icount > 0)
                    {
                        sbJson.Append(",");
                    }
                    sbJson.Append("{");
                    sbJson.Append("\"type_id\":\"c3\"");
                    sbJson.Append(",\"type_nme\":\"" + "分类" + "\"");
                    sbJson.Append(",\"rows\":[");
                    DataSet dstem = oCDbAccess.QueryBySQL("SELECT DISTINCT TOP 20 ID=A.LEVEL_THREE_ID,NAME=B.LEVEL_THREE_NME FROM SHP_GOODS_INFO A LEFT OUTER JOIN SHP_GOODS_TYPE_LEVEL_THREE B ON A.LEVEL_THREE_ID=B.LEVEL_THREE_ID "
                         + (lsSql.Count > 0 ? "WHERE " + string.Join(" AND ", lsSql) : ""));
                    for (int i = 0; i < dstem.Tables[0].Rows.Count; i++)
                    {
                        if (i > 0)
                        {
                            sbJson.Append(",");
                        }
                        string strSelVal = msBsc.AppLogic.Cfg.HttpTools.stringToJson(dstem.Tables[0].Rows[i]["ID"].ToString());
                        string strSelTxt = msBsc.AppLogic.Cfg.HttpTools.stringToJson(dstem.Tables[0].Rows[i]["NAME"].ToString());
                        string strSelImg = "";
                        sbJson.Append("{");
                        sbJson.Append("\"val\":\"" + strSelVal + "\"");
                        sbJson.Append(",\"txt\":\"" + strSelTxt + "\"");
                        sbJson.Append(",\"img\":\"" + strSelImg + "\"");
                        sbJson.Append(",\"query\":\"" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(this.GetQueryStr("c3", dstem.Tables[0].Rows[i]["ID"].ToString())) + "\"");
                        sbJson.Append("}");
                    }
                    sbJson.Append("]");
                    sbJson.Append("}");
                    icount++;
                }
                else if (!lsSelLinkKey.ContainsKey("c2") && lsSelLinkKey.ContainsKey("c1"))
                {
                    if (icount > 0)
                    {
                        sbJson.Append(",");
                    }
                    sbJson.Append("{");
                    sbJson.Append("\"type_id\":\"c2\"");
                    sbJson.Append(",\"type_nme\":\"" + "分类" + "\"");
                    sbJson.Append(",\"rows\":[");
                    DataSet dstem = oCDbAccess.QueryBySQL("SELECT DISTINCT TOP 20 ID=A.LEVEL_TWO_ID,NAME=B.LEVEL_TWO_NME FROM SHP_GOODS_INFO A LEFT OUTER JOIN SHP_GOODS_TYPE_LEVEL_TWO B ON A.LEVEL_TWO_ID=B.LEVEL_TWO_ID "
                         + (lsSql.Count > 0 ? "WHERE " + string.Join(" AND ", lsSql) : ""));
                    for (int i = 0; i < dstem.Tables[0].Rows.Count; i++)
                    {
                        if (i > 0)
                        {
                            sbJson.Append(",");
                        }
                        string strSelVal = msBsc.AppLogic.Cfg.HttpTools.stringToJson(dstem.Tables[0].Rows[i]["ID"].ToString());
                        string strSelTxt = msBsc.AppLogic.Cfg.HttpTools.stringToJson(dstem.Tables[0].Rows[i]["NAME"].ToString());
                        string strSelImg = "";
                        sbJson.Append("{");
                        sbJson.Append("\"val\":\"" + strSelVal + "\"");
                        sbJson.Append(",\"txt\":\"" + strSelTxt + "\"");
                        sbJson.Append(",\"img\":\"" + strSelImg + "\"");
                        sbJson.Append(",\"query\":\"" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(this.GetQueryStr("c2", dstem.Tables[0].Rows[i]["ID"].ToString())) + "\"");
                        sbJson.Append("}");
                    }
                    sbJson.Append("]");
                    sbJson.Append("}");
                    icount++;
                }
                else if (!lsSelLinkKey.ContainsKey("c3") && !lsSelLinkKey.ContainsKey("c2") && lsSelLinkKey.ContainsKey("c1"))
                {
                    if (icount > 0)
                    {
                        sbJson.Append(",");
                    }
                    sbJson.Append("{");
                    sbJson.Append("\"type_id\":\"c1\"");
                    sbJson.Append(",\"type_nme\":\"" + "分类" + "\"");
                    sbJson.Append(",\"rows\":[");
                    DataSet dstem = oCDbAccess.QueryBySQL("SELECT DISTINCT TOP 20 ID=A.LEVEL_ONE_ID,NAME=B.LEVEL_ONE_NME FROM SHP_GOODS_INFO A LEFT OUTER JOIN SHP_GOODS_TYPE_LEVEL_ONE B ON A.LEVEL_ONE_ID=B.LEVEL_ONE_ID "
                         + (lsSql.Count > 0 ? "WHERE " + string.Join(" AND ", lsSql) : ""));
                    for (int i = 0; i < dstem.Tables[0].Rows.Count; i++)
                    {
                        if (i > 0)
                        {
                            sbJson.Append(",");
                        }
                        string strSelVal = msBsc.AppLogic.Cfg.HttpTools.stringToJson(dstem.Tables[0].Rows[i]["ID"].ToString());
                        string strSelTxt = msBsc.AppLogic.Cfg.HttpTools.stringToJson(dstem.Tables[0].Rows[i]["NAME"].ToString());
                        string strSelImg = "";
                        sbJson.Append("{");
                        sbJson.Append("\"val\":\"" + strSelVal + "\"");
                        sbJson.Append(",\"txt\":\"" + strSelTxt + "\"");
                        sbJson.Append(",\"img\":\"" + strSelImg + "\"");
                        sbJson.Append(",\"query\":\"" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(this.GetQueryStr("c1", dstem.Tables[0].Rows[i]["ID"].ToString())) + "\"");
                        sbJson.Append("}");
                    }
                    sbJson.Append("]");
                    sbJson.Append("}");
                    icount++;
                }


                if (1 == 1)
                {
                    DataSet dstem = oCDbAccess.QueryBySQL(@"
    SELECT TOP 100
    *
    FROM
    (
    SELECT  
    T.ATTRIBUTE_NME,ID=T.ATTR_TEMPLATE_ID,NAME=T.ATTRIBUTE_VALUE,IMG_URL='' 
    ,SEQ_NUM=ROW_NUMBER() OVER (PARTITION BY T.ATTR_TEMPLATE_ID ORDER BY T.ATTRIBUTE_VALUE ASC)
    FROM SHP_GOODS_INFO A 
    LEFT OUTER JOIN SHP_GOODS_ATTRIBUTE T ON A.GOODS_ID=T.GOODS_ID " + (dicAttr.Count <= 0 ? "" : " AND (" + "T.ATTR_TEMPLATE_ID NOT IN ('" + string.Join("','", dicAttr.Keys.ToList()) + "')" + ")") + @"
    LEFT OUTER JOIN SHP_GOODS_ATTRIBUTE_TEMPLATE C ON T.ATTR_TEMPLATE_ID=c.ATTR_TEMPLATE_ID AND C.IS_SEARCH_USE=1
    WHERE
    (C.ATTR_TEMPLATE_ID IS NOT NULL) AND (C.IS_SEARCH_USE=1) " + (lsSql.Count > 0 ? "AND " + string.Join(" AND ", lsSql) : "") + @"
    GROUP BY
    T.ATTRIBUTE_NME,T.ATTR_TEMPLATE_ID,T.ATTRIBUTE_VALUE
    ) AS V1
    WHERE V1.SEQ_NUM<21
    ORDER BY ID ASC
    ");
                    int item31 = 0;
                    for (int i = 0; i < dstem.Tables[0].Rows.Count; i++)
                    {
                        if (i == 0 || i > 0 && dstem.Tables[0].Rows[i]["ID"].ToString() != dstem.Tables[0].Rows[i - 1]["ID"].ToString())
                        {
                            if (icount > 0)
                            {
                                sbJson.Append(",");
                            }
                            sbJson.Append("{");
                            sbJson.Append("\"type_id\":\"attr\"");
                            sbJson.Append(",\"type_nme\":\"" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(dstem.Tables[0].Rows[i]["ATTRIBUTE_NME"].ToString()) + "\"");
                            sbJson.Append(",\"rows\":[");
                        }

                        if (item31 > 0)
                        {
                            sbJson.Append(",");
                        }
                        string strSelVal = msBsc.AppLogic.Cfg.HttpTools.stringToJson(dstem.Tables[0].Rows[i]["ID"].ToString());
                        string strSelTxt = msBsc.AppLogic.Cfg.HttpTools.stringToJson(dstem.Tables[0].Rows[i]["NAME"].ToString());
                        string strSelImg = "";
                        sbJson.Append("{");
                        sbJson.Append("\"val\":\"" + strSelVal + "\"");
                        sbJson.Append(",\"txt\":\"" + strSelTxt + "\"");
                        sbJson.Append(",\"img\":\"" + strSelImg + "\"");
                        sbJson.Append(",\"query\":\"" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(this.GetQueryStr(dstem.Tables[0].Rows[i]["ID"].ToString(), dstem.Tables[0].Rows[i]["NAME"].ToString())) + "\"");
                        sbJson.Append("}");
                        item31++;

                        if (i == dstem.Tables[0].Rows.Count - 1 || dstem.Tables[0].Rows.Count > 1 && dstem.Tables[0].Rows[i]["ID"].ToString() != dstem.Tables[0].Rows[i + 1]["ID"].ToString())
                        {
                            sbJson.Append("]");
                            sbJson.Append("}");
                            item31 = 0;
                            icount++;
                        }
                    }

                }

                sbJson.Append("]");

                return sbJson.ToString();
            }
        }

        public string GetVehicleFilter()
        {
            System.Text.StringBuilder sbJson = new System.Text.StringBuilder();

            sbJson.Append("{");
            sbJson.Append("\"type_id\":\"r\"");
            sbJson.Append(",\"type_nme\":\"" + "选购热点" + "\"");
            sbJson.Append(",\"rows\":[");
            string strSelVal = msBsc.AppLogic.Cfg.HttpTools.stringToJson("12345");
            string strSelTxt = strSelVal;
            string strSelImg = "";
            sbJson.Append("{");
            sbJson.Append("\"val\":\"" + strSelVal + "\"");
            sbJson.Append(",\"txt\":\"" + strSelTxt + "\"");
            sbJson.Append(",\"img\":\"" + strSelImg + "\"");
            sbJson.Append(",\"query\":\"" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(this.GetQueryStr("r", "123456")) + "\"");
            sbJson.Append("}");
            sbJson.Append("]");
            sbJson.Append("}");
            return sbJson.ToString();
        }

        /// <summary>
        /// 返回商品列表{}
        /// </summary>
        /// <param name="CurrPageNum"></param>
        /// <param name="strPageRowsCount"></param>
        /// <param name="strSort"></param>
        /// <returns></returns>
        public string GetGoodsList(string CurrPageNum, string strPageRowsCount, string strSort)
        {
            using (Epm.AppLogic.Framework.CDbAccess oCDbAccess = new Epm.AppLogic.Framework.CDbAccess())
            {
                System.Data.DataSet ds = oCDbAccess.QueryBySQL(@"
    SELECT TOP 1000
    A.GOODS_ID
    ,A.GOODS_NME
    ,A.MONTHLY_VOLUME
    ,A.IMG_URL
    ,P.PRICE_DESC
    ,A.GOODS_OLD_PRICE
    ,A.GOODS_STRIKE_PRICE
    ,A.COMM_TENANT_ID
    ,D.COMM_TENANT_NME
    FROM SHP_GOODS_INFO A  LEFT OUTER JOIN SHP_GOODS_TYPE_LEVEL_THREE B ON A.LEVEL_THREE_ID=B.LEVEL_THREE_ID  
    LEFT OUTER JOIN SHP_GOODS_NORMS_PUBLIC P ON A.GOODS_ID=P.GOODS_ID AND P.SEQ_NUM=0
    LEFT OUTER JOIN SHP_COMMERCIAL_TENANT_INFO D ON A.COMM_TENANT_ID=D.COMM_TENANT_ID 
    " + (lsSql.Count > 0 ? "WHERE " + string.Join(" AND ", lsSql) : "")
        );


                string strjsontem1 = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", ""
                    , (CurrPageNum == string.Empty ? iCurrPageNum : int.Parse(CurrPageNum))
                    , (strPageRowsCount == string.Empty ? 20 : int.Parse(strPageRowsCount))
                    , (strSort == string.Empty ? strSorting : strSort));

                return strjsontem1;
            }
        }

        public string GetVehicleList(string CurrPageNum, string strPageRowsCount, string strSort)
        {
            using (Epm.AppLogic.Framework.CDbAccess oCDbAccess = new Epm.AppLogic.Framework.CDbAccess())
            {
                System.Data.DataSet ds = oCDbAccess.QueryBySQL(@"SELECT TOP 100 * FROM DJK_VEHICLE_INFO");


                string strjsontem1 = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", ""
                    , (CurrPageNum == string.Empty ? iCurrPageNum : int.Parse(CurrPageNum))
                    , (strPageRowsCount == string.Empty ? 20 : int.Parse(strPageRowsCount))
                    , (strSort == string.Empty ? strSorting : strSort));

                return strjsontem1;
            }
        }

        //                if (dicSql.ContainsKey("c1"))
        //                {
        //    //ID=CASE WHEN A.LEVEL_ONE_ID IN ('" + dicQuery["c"].Replace(",","','") + @"') THEN A.LEVEL_ONE_ID ELSE (CASE WHEN B.LEVEL_TWO_ID IN ('" + dicQuery["c"].Replace(",","','") + @"') THEN B.LEVEL_TWO_ID ELSE C.LEVEL_THREE_ID END) END
        //                    DataSet dstem = oCDbAccess.QueryBySQL(@"
        //    SELECT 
        //    ,NME=dbo.strJoin(CASE WHEN A.LEVEL_ONE_ID IN ('" + dicQuery["c"].Replace(",", "','") + @"') THEN A.LEVEL_ONE_NME ELSE (CASE WHEN B.LEVEL_TWO_ID IN ('" + dicQuery["c"].Replace(",", "','") + @"') THEN B.LEVEL_TWO_NME ELSE C.LEVEL_THREE_NME END) END)
        //    FROM SHP_GOODS_TYPE_LEVEL_ONE A
        //    LEFT OUTER JOIN SHP_GOODS_TYPE_LEVEL_TWO B ON A.LEVEL_ONE_ID=B.LEVEL_ONE_ID
        //    LEFT OUTER JOIN SHP_GOODS_TYPE_LEVEL_THREE C ON B.LEVEL_TWO_ID=C.LEVEL_TWO_ID
        //    WHERE
        //    (A.LEVEL_ONE_ID IN ('" + dicQuery["c"].Replace(",","','") + @"') OR B.LEVEL_TWO_ID IN ('" + dicQuery["c"].Replace(",","','") + @"') OR C.LEVEL_THREE_ID IN ('" + dicQuery["c"].Replace(",","','") + @"'))
        //    ");
        //                    if (dstem.Tables[0].Rows.Count > 0)
        //                    {
        //                        string strSelTxt = msBsc.AppLogic.Cfg.HttpTools.stringToJson(dstem.Tables[0].Rows[0]["NME"].ToString());
        //                        sbJson.Append("{");
        //                        sbJson.Append("\"type\":\"c\"");
        //                        sbJson.Append(",\"val\":\""+msBsc.AppLogic.Cfg.HttpTools.stringToJson(dicQuery["c"])+"\"");
        //                        sbJson.Append(",\"txt\":\"" + strSelTxt + "\"");
        //                        sbJson.Append(",\"del_query\":\"" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(this.GetQueryStr("c","")) + "\"");
        //                        sbJson.Append("}");
        //                        icount++;
        //                    }
        //                }
    }
}
